﻿@using GoodMan.Infrastructure.Entities.Entity;
@using GoodMan.Infrastructure.Entities.Contract;
@using GoodMan.Tool.Framework;
@{
    ViewBag.Title = "快餐明细";
    Layout = "~/Areas/WorkSpace/Views/Shared/_CollegeAdminTemplete.cshtml";
    List<ProductCategory> categoryList = ViewBag.Categorys;

    int productId = ViewBag.productiId == null ? 0 : ViewBag.productiId;
    Product product = ViewBag.product == null ? new Product() : ViewBag.product;
    Policy policy = ViewBag.policy == null ? new Policy() : ViewBag.policy;
    List<ProductRepertory> productRepertorys = ViewBag.productRepertorys;
    List<Business> businessList = ViewBag.Business;
}
@section head{
    <style type="text/css">
        .nav .active
        {
            /*background: url(<?php echo $this->img('manage_r2_c13.jpg');?>) no-repeat;*/
            background-color: #F5F5F5;
        }
        .nav .other
        {
            /* background: url(<?php echo $this->img('manage_r2_c14.jpg');?>) no-repeat;*/
            background-color: #E9E9E9;
        }
        h2.nav
        {
            border-bottom: 1px solid #B4C9C6;
            font-size: 13px;
            height: 25px;
            line-height: 25px;
            margin-top: 0px;
            margin-bottom: 0px;
        }
        h2.nav a
        {
            color: #999999;
            display: block;
            float: left;
            height: 24px;
            width: 113px;
            text-align: center;
            margin-right: 1px;
            margin-left: 1px;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        function show_hide(id) {
            len = $('.nav a').length;
            if (len > 1) {
                for (i = 1; i <= len; i++) {
                    if (i == id) {

                        $("#a" + id).css('background-color', '#F5F5F5');
                        $("#tab" + id).css('display', 'block');
                    } else {
                        $("#a" + i).css('background-color', '#E9E9E9');
                        $("#tab" + i).css('display', 'none');
                    }
                }
            }
        }
        function delImg() {
            $('#ImagePaths').attr('value', "");
            $('#container').html("");
        }
        function delImg2(id) {
            $.post("/CollegeAdmin/DelProductImg", { Id: id }, function (response) {
                $('#ImagePaths').attr('value', "");
                $('#container').html("");
            });
        }
       
    </script>
    <script type="text/javascript" src="/Scripts/jquery.js"></script>
    <script type="text/javascript" src="/Scripts/plug.js"></script>
    <link type="text/css" rel="Stylesheet" href="/content/css/select2.css" />
}
<div id="content-header">
    <h1>
        快餐</h1>
</div>
<div id="breadcrumb">
    <a href="#" title="Go to Home" class="tip-bottom"><i class="icon-home"></i>首页</a>
    <a href="#" class="tip-bottom">快餐管理</a> <a href="/workspace/CollegeAdmin/GeneralProducts"
        class="current">快餐</a> <a href="#" class="current">添加/编辑</a>
</div>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div class="widget-box">
                <div class="widget-title">
                    <span class="icon"><i class="icon-align-justify"></i></span>
                    <h5>
                        快餐</h5>
                </div>
                <div class="widget-content nopadding">
                    <form action="/CollegeAdmin/SaveGeneralFastFood" method="post" id="productDetailForm"
                    class="form-horizontal">
                    <input type="hidden" name="page" value="@ViewBag.Page" />
                    <input type="hidden" name="productkey" value="@ViewBag.productkey" />
                    <div class="control-group">
                        <h2 class="nav">
                            <a id="a1" style="background-color: #F5F5F5" onclick="show_hide('1'); return false;">
                                通用属性</a>  <a id="a3" style="background-color: #E9E9E9" onclick="show_hide('3'); return false;">
                                        快餐描述</a>
                        </h2>
                    </div>
                    <div id="tab1">
                        <input type="hidden" id="ProductModel.Id" name="ProductModel.Id" value="@productId" />
                        @{
                            if (productId > 0)
                            {
                            <div class="control-group">
                                <label class="control-label">
                                    所属商户</label>
                                <div class="controls">
                                    <select id="business" name="ProductModel.BusinessId" style="width: 170px">
                                        @{
                                if (businessList != null && businessList.Count != 0)
                                {
                                    string selectStr = " selected=\"selected\"";
                                    foreach (var item in businessList)
                                    {
                                        if (item.Id == product.BusinessId)
                                        {
                                            selectStr = " selected=\"selected\"";
                                        }
                                        else
                                        {
                                            selectStr = "";
                                        }
                                            <option value="@item.Id" @selectStr>@item.Name</option>
                                    }

                                }
                                else
                                {
                                            <option value="-1">请添加商户</option>
                                }
                                        }
                                    </select>
                                    <span style="color:Red;font-size:20px">&nbsp;&nbsp;*</span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">
                                    快餐名称</label>
                                <div class="controls">
                                    <input type="text" value="@product.ProductName" class="span4 {required:true}" placeholder="输入快餐名称" id="ProductModel.ProductName" name="ProductModel.ProductName" />
                                <span style="color:Red;font-size:20px">&nbsp;&nbsp;*</span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">
                                    快餐图片</label>
                                <div class="controls">
                                    <input type="hidden" value="@product.Images"  class="span4 {required:true}"  id="ImagePaths" name="ProductModel.Images"/>
                                    <button class="btn btn-warning btn-mini" value="click" onclick="LoadUploadControl();return false;">
                                        点击选择图片</button>&nbsp;&nbsp;&nbsp;<button class="btn btn-danger btn-mini" value="dclick" onclick="delImg2("@productId");return false;">
                                            删除</button><div id="container">
                                            @{
                                if (product.Images != null && product.Images != "")
                                {
                                    string[] imageArray = product.Images.Split(';');
                                    foreach (var item in imageArray)
                                    {
                                        if (item != "")
                                        {
                                                <img alt="@product.ProductCode" src="@item" style="width:115px;height:95px;margin-left:10px;margin-top:10px;"/>
                                        }
                                    }
                                }
                                            }
                                        </div>
                                </div>
                            </div>                                
                            
                            <div class="control-group">
                                <label class="control-label">
                                    销售价</label>
                                <div class="controls">
                                    <input type="text" value="@policy.Price"  class="span4 {required:true,number:true,min:0.1}" id="ProductModel.SalePrice" name="ProductModel.SalePrice"
                                            placeholder="输入销售价" style="width: 100px" /><input type="hidden" id="ProductModel.PolicyId" name="ProductModel.PolicyId" value="@policy.Id" />&nbsp;&nbsp;<span style="color:#FF0000">无配菜时销售价，有配菜时可不输入</span>
                                </div>
                            </div>
                            }
                            else
                            {
                            <div class="control-group">
                                <label class="control-label">
                                    所属商户</label>
                                <div class="controls">
                                    <select id="business" name="ProductModel.BusinessId"  class="span4 {required:true}" style="width: 170px">
                                        @{
                                if (businessList != null && businessList.Count != 0)
                                {
                                    foreach (var item in businessList)
                                    {
                                            <option value="@item.Id" >@item.Name</option>
                                    }

                                }
                                else
                                {
                                            <option value="-1">请添加商户</option>
                                }
                                        }
                                    </select>
                                    <span style="color:Red;font-size:20px">&nbsp;&nbsp;*</span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">
                                    快餐名称</label>
                                <div class="controls">
                                    <input type="text" value="" placeholder="输入快餐名称"  class="span4 {required:true}" id="ProductModel.ProductName" name="ProductModel.ProductName" />
                                <span style="color:Red;font-size:20px">&nbsp;&nbsp;*</span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">
                                    快餐图片</label>
                                <div class="controls">
                                    <input type="hidden" value="" id="ImagePaths"  class="span4 {required:true}" name="ProductModel.Images" />
                                    <button class="btn btn-warning btn-mini" value="click" onclick="LoadUploadControl();return false;">
                                        点击选择图片</button>&nbsp;&nbsp;&nbsp;<button class="btn btn-danger btn-mini" value="dclick" onclick="delImg();return false;">
                                            删除</button><div id="container">
                                        </div>
                                </div>
                            </div>
                                
                            <div class="control-group">
                                <label class="control-label">
                                    销售价</label>
                                <div class="controls">
                                    <input type="text" value="" id="ProductModel.SalePrice" class="{number:true,min:0.1,messages:{number:'请输入价格',min:'必须大于0.1'}}" name="ProductModel.SalePrice"
                                        placeholder="输入销售价" style="width: 100px" /><input type="hidden" id="ProductModel.PolicyId"
                                            name="ProductModel.PolicyId" value="0" />&nbsp;&nbsp;<span style="color:#FF0000">无配菜时销售价，有配菜时可不输入</span>
                                </div>
                            </div>
                            }
                        }
                  
                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                            @{
                                if (productId > 0)
                                {
                                    if (!string.IsNullOrEmpty(product.Catering))
                                    {
                                        CateringSet catering = product.Catering.ToObject<CateringSet>();
                                        if (catering != null && catering.cateringList != null && catering.cateringList.Any())
                                        {
                                            int j = 0;//控制显示+—
                                            string temp = "<a href=\"javascript:;\" class=\"addVer\">[+]</a> ";
                                            string verValue = "";
                                            foreach (var cItem in catering.cateringList)
                                            {
                                                verValue = "";
                                                if (j != 0)
                                                {
                                                    temp = "<a href=\"javascript:;\" class=\"removeVer\">[-]</a>";
                                                }

                                                foreach (var ciItem in cItem.cateringInfoList)
                                                {
                                                    verValue += ciItem.caterName + "|" + ciItem.caterPrice + "#";
                                                }
                                                if (verValue.Length > 0)
                                                {
                                                    verValue = verValue.Substring(0, verValue.Length - 1);
                                                }
                                <tr>
                                    <td valign="top" width="40%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                @Html.Raw(@temp)
                                                配菜名称</label>
                                            <div class="controls">
                                                <input type="text" name="ProductModel.VerName" value="@cItem.caterCategory" placeholder="输入配菜名称"  />
                                            </div>
                                        </div>
                                    </td>
                                    <td valign="top" width="56%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                配菜属性</label>
                                            <div class="controls" style="vertical-align: top">
                                                <textarea name="ProductModel.VerValue" style="width: 55%">@verValue</textarea>&nbsp;&nbsp;每个属性用"|"分割
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                            }
                                        }
                                        else
                                        {
                                            //无配菜情况
                                <tr>
                                    <td valign="top" width="40%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                <a href="javascript:;" class="addVer">[+]</a> 配菜名称</label>
                                            <div class="controls">
                                                <input type="text" name="ProductModel.VerName" placeholder="输入配菜名称" />
                                            </div>
                                        </div>
                                    </td>
                                    <td valign="top" width="56%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                配菜属性</label>
                                            <div class="controls" style="vertical-align: top">
                                                <textarea name="ProductModel.VerValue" style="width: 60%"></textarea>&nbsp;&nbsp;每个属性用"|"分割
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                        }


                                    }
                                    else
                                    {
                                <tr>
                                    <td valign="top" width="40%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                <a href="javascript:;" class="addVer">[+]</a> 配菜名称</label>
                                            <div class="controls">
                                                <input type="text" name="ProductModel.VerName" value="" placeholder="输入配菜名称" />
                                            </div>
                                        </div>
                                    </td>
                                    <td valign="top" width="56%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                配菜属性</label>
                                            <div class="controls" style="vertical-align: top">
                                                <table border="0">
                                                    <tr>
                                                        <td width="70%">
                                                            <textarea name="ProductModel.VerValue" style="width: 85%"></textarea>&nbsp;&nbsp;
                                                        </td>
                                                        <td>
                                                            <span>每个属性用"|"分割<br />
                                                                <font color="#ff0000">格式:名称|价格</font><br />
                                                                如&nbsp;白菜|5</span>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                    }
                                }
                                else
                                {
                                <tr>
                                    <td valign="top" width="30%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                <a href="javascript:;" class="addVer">[+]</a> 配菜名称</label>
                                            <div class="controls">
                                                <input type="text" name="ProductModel.VerName" placeholder="输入配菜名称" />
                                            </div>
                                        </div>
                                    </td>
                                    <td valign="top" width="66%">
                                        <div class="control-group">
                                            <label class="control-label">
                                                配菜属性</label>
                                            <div class="controls" style="vertical-align: top">
                                                <table border="0" width="100%">
                                                    <tr>
                                                        <td width="50%">
                                                            <textarea name="ProductModel.VerValue" style="width: 90%"></textarea>&nbsp;&nbsp;
                                                        </td>
                                                        <td>
                                                            <span>每个属性用"|"分割<br />
                                                                <font color="#ff0000">格式:名称|价格#名称|价格</font><br />
                                                                如&nbsp;白菜|5#黄瓜|5</span>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                }
                            }
                        </table>
                    </div>
                    <div id="tab3" style="display: none;">
                        @{
                            if (productId > 0)
                            {
                            <div class="control-group">
                                <label class="control-label">
                                    快餐描述</label>
                                <div class="controls">
                                    <script id="editor" type="text/plain" name="ProductModel.Description">@Html.Raw(@product.Description)</script>
                                    <script type="text/javascript" charset="utf-8" src="/Scripts/UEditor/editor_config.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="/Scripts/UEditor/editor_all_min.js"></script>
                                    <script type="text/javascript">
                                        UE.getEditor('editor');
                                    </script>
                                </div>
                            </div>
                            }
                            else
                            {
                            <div class="control-group">
                                <label class="control-label">
                                    快餐描述</label>
                                <div class="controls">
                                    <script id="editor" type="text/plain" name="ProductModel.Description"></script>
                                    <script type="text/javascript" charset="utf-8" src="/Scripts/UEditor/editor_config.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="/Scripts/UEditor/editor_all_min.js"></script>
                                    <script type="text/javascript">
                                        UE.getEditor('editor');
                                    </script>
                                </div>
                            </div>
                            }
                        }
                    </div>
                    <div>
                        @{
                                         
                            if (ViewBag.isSuccess != null)
                            {
                                if (ViewBag.isSuccess)
                                { 
                            <div class="alert alert-success">
                                <strong>快餐保存成功！</strong>
                            </div>
                                                 
                                }
                                else
                                {
                            <div class="alert alert-error">
                                <strong>快餐保存失败！</strong>
                            </div>
                                                 
                                }
                            }
                        }
                        <div class="alert alert-error" id="pcinfo">配菜信息输入不符合格式！</div>
                        <div class="form-actions">
                            <button type="button" id="btnSubmit" class="btn btn-primary" onclick="return validatePictrue();">
                                保存</button>
                        </div>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!--模态弹出框-->
<div id="fileuploadframe" style="display: none; width: 550px; height: 481px;">
    <div style="height: 25px; width: 100%; border-bottom: 1px solid #ccc">
        <div style="float: right; width: 80px; height: 23px; line-height: 23px;">
            <a href="javascript:;">
                <img alt="关闭" style="border: none;" id="btnflfclose" src="/content/img/closelabel.gif" /></a>
        </div>
    </div>
    <div style="width: 100%; height: 455px;">
        <div id="fileuploadframe_imgbox" style="width: 100%; height: 410px; overflow: auto;">
        </div>
        <div id="fileuploadframe_controlbox" style="width: 100%; height: 45px;">
            <div id="fileupload" style="float: left; width: 130px;">
            </div>
            <div id="fileuploadframe_hint" style="font-size: 12px; padding-top: 20px; float: left;
                width: 320px;">
            </div>
            <div id="fileuploadframe_btnsubmit" style="float: right;">
                <input id="btnupload" type="button" class="btn btn-primary btn-mini" value="完成" /></div>
        </div>
    </div>
</div>
<script type="text/javascript" src="/scripts/jquery.js"></script>
<script type="text/javascript" src="/Scripts/plug.js"></script>
<script type="text/javascript" src="/scripts/swfupload/swfupload.js"></script>
<script type="text/javascript" src="/scripts/swfupload/jquery.swfupload.js"></script>
<script type="text/javascript">
    //多图片上传
    var swf = new plug.SwfUpload();
    function LoadUploadControl() {
        new plug.PopFrame().OpenFrame('fileuploadframe', 'btnflfclose', function () {
            swf.Bind('fileupload', {
                upload_url: "/home/SWFPrepareUpload",
                file_types: "*.jpg;*.png",
                file_types_description: "jpg或png格式",
                commitbtn: 'btnupload',
                commiturl: '/home/SWFUpload',
                commitdata: '',
                onDialogStart: function () {
                    $('#fileuploadframe_imgbox').children('#uploadframe_nophotobox').remove();
                },
                onLoad: function () {
                    $('#fileuploadframe_imgbox').append(
                            $('<div id="uploadframe_nophotobox" style="width:130px; padding-top:120px;margin-left:auto; margin-right:auto;"/>').append(
                                $('<img src="/scripts/swfupload/no_photo.png" alt="noPhoto" />')
                            )
                        );
                    $('#fileuploadframe_hint').text('请选择不大于5MB的 PNG，JPG图片。');
                },
                onAppend: function (event, file) {
                    $('#fileuploadframe_imgbox').append(
                            $('<div id="' + file.name.replace('.', '_') + '" style="margin-right:8px;margin-top:5px;float:left;width:113px;height:113px;padding:5px;border:1px solid #ccc;"></div>')
                                .append($('<div style="width:20px;margin-left:auto;margin-right:auto;padding-top:50px;"><img alt="loading" src="/scripts/swfupload/frameload.gif" /></div>'))
                        );
                },
                onError: function (event, file, errorCode, message) {
                    if (errorCode == -110) {
                        alert('文件：' + file.name + '过大，请选择不超过7MB的文件');
                    }
                    else {
                        alert('文件：' + file.name + '上传失败，请稍后再试');
                        var id = file.name.replace('.', '_');
                        if ($('#' + id)) {
                            $('#' + id).remove();
                        }
                    }
                },
                onSuccess: function (event, file, serverData) {
                    var data = eval('(' + serverData + ')');
                    var id = file.name.replace('.', '_');
                    if ($('#' + id)) {
                        $('#' + id).attr('id', data.name.replace('.', '_'))
                                .html('<div><img style="width:115px;height:95px;" alt="' + data.name.replace('.', '_') + '" src="' + data.path + '" /></div>' +
                                '<div style="padding-top:2px;float:right;cursor:pointer;" onclick="javascript:swf.DeleteItem(this);"><img alt="删除" style="width:16px;height:16px;" src="/scripts/swfupload/Trash_Delete.png" /></div>');
                        swf.PutImage({ name: data.name, path: data.path, horizontal: data.horizontal.toString() });
                    }
                },
                commitComplete: function (files) {
                    $('#btnflfclose').trigger('click');
                    var imgs = $('#ImagePaths').attr('value');
                    var width = 95, height = 115;
                    for (var i = 0, len = files.length; i < len; i++) {
                        if (/^true$/i.test(files[i].horizontal.toString())) {
                            width = 115;
                            height = 95;
                        }
                        $('#container').append(
                                $('<img alt="' + files[i].name + '" src="' + files[i].path + '" style="width:' + width + 'px;height:' + height + 'px;margin-left:10px;margin-top:10px;"/></div>'));
                        imgs += files[i].path + ';';
                    }
                    $('#ImagePaths').attr('value', imgs);
                }
            });
        }, function () {
            $('#fileuploadframe_imgbox').html('');
            $('#fileupload').html('');
            swf.uninstall();
        });
    }
</script>
@section tail{
    <script type="text/javascript" src="/scripts/jquery.js"></script>
    <script type="text/javascript" src="/Scripts/plug.js"></script>
    <script type="text/javascript" src="/scripts/swfupload/swfupload.js"></script>
    <script type="text/javascript" src="/scripts/swfupload/jquery.swfupload.js"></script>
    <script type="text/javascript" src="/scripts/select2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#pcinfo').hide();
            $('#likProduct').addClass('open');
            $('#likProduct').addClass('active');
            $('#business').each(function () {
                $(this).select2();
            });
        });
        $('.addVer').live('click', function () {

            str = $(this).parent().parent().parent().parent().html();
            //alert(str);
            str = str.replace('addVer', 'removeVer');
            str = str.replace('[+]', '[-]');
            //str = str.replace(/goodspackage/g, 'goodspackage' + rand); //正则表达式替换多个
            $(this).parent().parent().parent().parent().parent().append('<tr>' + str + '</tr>');

        });
        $('.removeVer').live('click', function () {
            $(this).parent().parent().parent().parent().remove();
            return false;
        });
    </script>

    <script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.metadata.js"></script>
    <script type="text/javascript" src="/Scripts/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript">
        var validation = new plug.BindValidCode();
        $(function () {
            validation.BindValidator();

            $('#productDetailForm').validate({
                errorClass: "help-inline",
                errorElement: "span",
                highlight: function (element, errorClass, validClass) {
                    $(element).parents('.control-group').addClass('error');
                },
                unhighlight: function (element, errorClass, validClass) {
                    $(element).parents('.control-group').removeClass('error');
                }
            });
            $('#btnSubmit').click(function () {
                var flag = true;
                var _reg = /^[0-9A-Za-z\u4e00-\u9fa5]+\|\d+(\.\d+)?(#[0-9A-Za-z\u4e00-\u9fa5]+\|\d+(\.\d+)?)*$/;
                $('textarea[name="ProductModel.VerValue"]').each(function () {
                    if ($(this).val().length != 0 && !_reg.test($(this).val())) {
                        flag = false;
                    }
                });
                if (!flag) {
                    $('#pcinfo').show();
                    setTimeout(function () {  $('#pcinfo').hide(); }, 2000);
                    return;
                }
                $('#productDetailForm').submit();
            });
        });
        function Refalsh(sender) {
            validation.Refalsh(sender);
        }

        
        function validatePictrue() {
            if ($('#ImagePaths').val() == '') {
                alert('请添加图片！');
                return false;
            }
            return true;
        }
    </script>
}
